<?php
namespace app\api\controller\wine;

use app\api\controller\Controller;
use app\api\model\wine\Dermember;
use app\api\service\Statistics as StatisticsService;
use app\api\model\wine\DermemberProfitBill;
use think\App;

class Statistics extends Controller{
    private $userId ;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $userArr = $this->getLoginUser();
        $this->userId = $userArr['user_id'];
    }
    // 首页
    public function index(){
        StatisticsService::setTime();
        // 交易额
        $trade = [
            'allIncome' => StatisticsService::allIncome($this->userId),
            'directIncome' => StatisticsService::directIncome($this->userId),
            'offlineIncome' => StatisticsService::offlineIncome($this->userId)
        ];
        // 商户新增
        $merchant = [
            'merchantAdd' => StatisticsService::merchantAdd(),
            'merchantMy' => StatisticsService::merchantAdd($this->userId),
        ];
        // 达标数据
        $standard = StatisticsService::UpToStandard($this->userId);
        $sum = [
            'trade' => $trade,
            'merchant' => $merchant,
            'standard' => $standard
        ];
        return $this->renderSuccess(compact('sum'));
    }

    private function lastMonth(){
        $i = 1;
        return date( 'Y-m', strtotime( 'last day of -'.$i.' months' ) ).'-01';
    }
    // 流水
    public function cashFlow(){
        $cashFlow = DermemberProfitBill::cashFlow($this->userId);
        $cashFlowAll = DermemberProfitBill::cashFlowAll($this->userId);
        $thisMonth = date('Y-m-01');
        $lastMonthFlow = DermemberProfitBill::cashFlowTime($this->userId,$this->lastMonth(),$thisMonth);
        $cash = [
            'cashFlow' => $cashFlow,
            'cashFlowAll' => $cashFlowAll,
            'lastMonthFlow' => $lastMonthFlow
        ];
        return $this->renderSuccess(compact('cash'));
    }

    // 按时间段查询流水
    public function findCashFlow(){
        $startTime = input('startTime','','string');
        $endTime = input('endTime','','string');
        $cashFlow = DermemberProfitBill::cashFlowTime($this->userId,$startTime,$endTime);
        return $this->renderSuccess(compact('cashFlow'));
    }

    // 分润
    public function profit(){
        $cashFlow = DermemberProfitBill::shareFlow($this->userId);
        $cashFlowAll = DermemberProfitBill::shareFlowAll($this->userId);
        $thisMonth = date('Y-m-01');
        $lastMonthFlow = DermemberProfitBill::shareTime($this->userId,$this->lastMonth(),$thisMonth);
        $cash = [
            'cashFlow' => $cashFlow,
            'cashFlowAll' => $cashFlowAll,
            'lastMonthFlow' => $lastMonthFlow
        ];
        return $this->renderSuccess(compact('cash'));
    }

    // 按时间段查询分润
    public function findProfit(){
        $startTime = input('startTime','','string');
        $endTime = input('endTime','','string');
        $profitFlow = DermemberProfitBill::shareTime($this->userId,$startTime,$endTime);
        return $this->renderSuccess(compact('profitFlow'));
    }

    // 本月商户明细
    public function merchMonth(){
        $derModel = new Dermember();
        $list = $derModel->merchMonth();
        return $this->renderSuccess(compact('list'));
    }

    // 我的经销商
    public function myder(){
        $keyword = input('keyword','','string');
        $derModel = new Dermember();
        $list = $derModel->myder($keyword);
        return $this->renderSuccess(compact('list'));
    }
}
